package web.servlet;

import domain.Account;
import domain.Comment;
import service.CommentService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "CommentServlet", urlPatterns = {"/Comment"})
public class CommentServlet extends HttpServlet{

    private CommentService productService = new CommentService();

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
         String commentInfo =req.getParameter("comment");
         Account user=(Account)req.getSession().getAttribute("loginUser");
         if(user!=null){
             String user_id= user.getUserid();
             Comment comment=new Comment();
             comment.setUserid(user_id);
             comment.setCommentInfo(commentInfo);
             productService.addComment(comment);
         }else{
             Comment comment=new Comment();
             comment.setUserid("游客");
             comment.setCommentInfo(commentInfo);
             productService.addComment(comment);
         }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
